#!/bin/bash -e

# NOTE: this is a quick backport of the 2.11.x script to 2.10.x -- some comments may be out dated

# requirements:
# - ~/.sonatype-curl that consists of user = USER:PASS
# - ~/.credentials (for sonatype)
    # realm=Sonatype Nexus Repository Manager
    # host=oss.sonatype.org
    # user=lamp
    # password=

# publishToSonatype
# set to anything but "yes" to avoid publishing to sonatype
# overridden to "no" when no SCALA_VER_BASE is passed and HEAD is not tagged with a valid version tag
#

baseDir=${WORKSPACE-`pwd`}
scriptsDir="$baseDir/scripts"
. $scriptsDir/common



##### sonatype interface

stApi="https://oss.sonatype.org/service/local"

function st_curl(){
  curl -H "Content-Type: application/json" -H "accept: application/json,application/vnd.siesta-error-v1+json,application/vnd.siesta-validation-errors-v1+json"  -K ~/.sonatype-curl -s -o - $@
}

function st_stagingReposOpen() {
 st_curl "$stApi/staging/profile_repositories" | jq '.data[] | select(.profileName == "org.scala-lang") | select(.type == "open")'
}

function st_stagingRepoDrop() {
  repo=$1
  message=$2
  echo "{\"data\":{\"description\":\"$message\",\"stagedRepositoryIds\":[\"$repo\"]}}" | st_curl -X POST -d @- "$stApi/staging/bulk/drop"
}

function st_stagingRepoClose() {
  repo=$1
  message=$2
  echo "{\"data\":{\"description\":\"$message\",\"stagedRepositoryIds\":[\"$repo\"]}}" | st_curl -X POST -d @- "$stApi/staging/bulk/close"
}


## BUILD STEPS:

determineScalaVersion() {
  cd $WORKSPACE
  if [ -z "$SCALA_VER_BASE" ]; then
    echo "No SCALA_VER_BASE specified."

    scalaTag=$(git describe --exact-match ||:)

    SCALA_BINARY_VER=${SCALA_BINARY_VER-"$scala_binary_version"}

    if [ -z "$scalaTag" ]
    then
      echo "No tag found, building nightly snapshot."
      parseScalaProperties "build.number"
      SCALA_VER_BASE="$version_major.$version_minor.$version_patch"
      SCALA_VER_SUFFIX="-$(git rev-parse --short HEAD)-nightly"
      SCALADOC_SOURCE_LINKS_VER=$(git rev-parse HEAD)

      # TODO: publish nightly snapshot using this script
      publishToSonatype="no"
      echo "repo_ref=2.11.x" >> $baseDir/jenkins.properties # for the -dist downstream jobs that build the actual archives
    else
      echo "HEAD is tagged as $scalaTag."
      # borrowed from https://github.com/cloudflare/semver_bash/blob/master/semver.sh
      local RE='v*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' # don't change this to make it more accurate, it's not worth it
      SCALA_VER_BASE="$(echo $scalaTag | sed -e "s#$RE#\1.\2.\3#")"
      SCALA_VER_SUFFIX="$(echo $scalaTag | sed -e "s#$RE#\4#")"
      SCALADOC_SOURCE_LINKS_VER=$scalaTag

      if [ "$SCALA_VER_BASE" == "$scalaTag" ]; then
        echo "Could not parse version $scalaTag"
        exit 1
      fi
      publishToSonatype=${publishToSonatype-"yes"} # unless forced previously, publish
    fi
  else
    publishToSonatype=${publishToSonatype-"yes"} # unless forced previously, publish
    # if version base/suffix are provided, we assume a corresponding tag exists for the scaladoc source links
    SCALADOC_SOURCE_LINKS_VER="v$SCALA_VER_BASE$SCALA_VER_SUFFIX"
  fi

  SCALA_VER="$SCALA_VER_BASE$SCALA_VER_SUFFIX"
  echo "version=$SCALA_VER" >> $baseDir/jenkins.properties
  echo "sbtDistVersionOverride=-Dproject.version=$SCALA_VER" >> $baseDir/jenkins.properties

  echo "Building Scala $SCALA_VER."
}

distro() {
  cd $WORKSPACE

  echo "### Building the distribution"

  ant -Dmaven.version.number=$SCALA_VER\
      -Dbuild.release=1\
      -Dscaladoc.git.commit=$SCALADOC_SOURCE_LINKS_VER\
      distpack-opt

  (cd $WORKSPACE/dists/ && tar cvzhf ../scala-dist-$SCALA_VER.tar.gz .)
  s3Upload scala-dist-$SCALA_VER.tar.gz
}

s3Upload() {
  file="$1"
  contentType="application/x-compressed-tar"
  host=$(grep host ~/.s3credentials | cut -d= -f2)

  s3curl --id typesafe --contentType "${contentType}" --put "${file}" -- -k https://${host}/scala/tmp/${file}
}

# assumes we just bootstrapped, and current directory is $baseDir
# publishes locker to sonatype, then builds modules again (those for which version numbers were provided),
# and publishes those to sonatype as well
# finally, the staging repos are closed
publishSonatype() {
  # stage to sonatype, along with all modules -Dmaven.version.suffix/-Dbuild.release not necessary,
  # since we're just publishing an existing build
  echo "### Publishing to sonatype"
  (cd $WORKSPACE/dists/maven/latest/ && ant deploy.signed)

  open=$(st_stagingReposOpen)
  allOpenUrls=$(echo $open | jq  '.repositoryURI' | tr -d \")
  allOpen=$(echo $open | jq  '.repositoryId' | tr -d \")

  echo "Closing open repos: $allOpen"

  for repo in $allOpen; do st_stagingRepoClose $repo; done

  echo "Closed sonatype staging repos: $allOpenUrls."
}


#### MAIN

determineScalaVersion

distro

if [ "$publishToSonatype" == "yes" ]
  then publishSonatype
fi
